/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bitaka.ws;

import entity.Billet;
import entity.Onligne;
import entity.Trans;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DBTransaction {

    ConnectBase maBase;
    Connection conn;
    Statement st = null;

    public DBTransaction() {
        maBase = new ConnectBase();
        maBase.dbConnect();
        conn = maBase.getConn();
    }

    public void insertTrans(Trans t) throws ParseException {
        System.out.println("enter trans");
        try {
            st = conn.createStatement();
        } catch (SQLException ex) {
            System.out.println("conn " + ex);
            Logger.getLogger(VerifyPass.class.getName()).log(Level.SEVERE, null, ex);
        }
        // System.out.println("avant date");
        SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat DATE_FORMAT2 = new SimpleDateFormat("hh:mm:ss");
        String date = DATE_FORMAT.format(t.getDate());
        String date2 = DATE_FORMAT2.format(t.getDate());


        // System.out.println("xxxxx" + date);
        String req2 = "insert into trans (\"time\",numero,date,etat,porte) values ('" + date2 + "','" + t.getNumero() + "','" + date + "','"
                + t.getEtat() + "','" + t.getPorte() + "')";
        try {
            System.out.println(req2);
            st.executeUpdate(req2);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void updateBillet(Billet t) throws ParseException {

        try {
            st = conn.createStatement();
        } catch (SQLException ex) {
            System.out.println("conn " + ex);
            Logger.getLogger(VerifyPass.class.getName()).log(Level.SEVERE, null, ex);
        }


        System.out.println("Updating Billet");
        String req2 = "update billet set utilise=" + t.getUtilise() + " , bnr_acces=" + t.getBnrAcces() + " where numero like '" + t.getNumero() + "'";
        try {
            System.out.println(req2);
            st.executeUpdate(req2);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
